import CryptoJS from "crypto-js";

const key = CryptoJS.enc.Utf8.parse("1234567890000000"); //16位
const iv = CryptoJS.enc.Utf8.parse("1234567890000000");

export default {
    //aes加密
    encrypt(word) {
        let encrypted = "";
        if (typeof word == "string") {
            const srcs = CryptoJS.enc.Utf8.parse(word);
            encrypted = CryptoJS.AES.encrypt(srcs, key, {
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding:CryptoJS.pad.Pkcs7
            });
        } else if (typeof word == "object") {
            //对象格式的转成json字符串
            const data = JSON.stringify(word);
            const srcs = CryptoJS.enc.Utf8.parse(data);
            encrypted = CryptoJS.AES.encrypt(srcs, key, {
                iv: iv,
                mode: CryptoJS.mode.CBC,
                padding:CryptoJS.pad.Pkcs7
            });
        }
        return encrypted.ciphertext.toString();
    },
    // aes解密
    decrypt(word) {
        // word = (word + '').replace(/\n*$/g, '').replace(/\n/g, ''); //增加这一行，将换行符替换为空
        var decrypt = CryptoJS.AES.decrypt(word, key, {
            iv: iv,
            mode: CryptoJS.mode.CBC,
            padding: CryptoJS.pad.Pkcs7
        });
        return JSON.parse(decrypt.toString(CryptoJS.enc.Utf8))
    },

};
